package com.controller;

import java.io.File;
import java.io.IOException;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.entity.Cihuixinxi;
import com.server.CihuixinxiServer;
import com.util.PageBean;
import net.sf.json.JSONObject;
import com.util.db;
import java.sql.SQLException;
import java.sql.*;
@Controller
public class CihuixinxiController {
	@Resource
	private CihuixinxiServer cihuixinxiService;

	@RequestMapping("addCihuixinxi.do")
	public String addCihuixinxi(HttpServletRequest request,Cihuixinxi cihuixinxi,HttpSession session) throws SQLException{
		Timestamp time=new Timestamp(System.currentTimeMillis());
		
		cihuixinxi.setAddtime(time.toString().substring(0, 19));
		cihuixinxiService.add(cihuixinxi);
		db dbo = new db();
		
		//kuabiaogaizhi
		session.setAttribute("backxx", "添加成功");session.setAttribute("backurl", request.getHeader("Referer"));return "redirect:postback.jsp";
		
	}
 
 
//	处理编辑
	@RequestMapping("doUpdateCihuixinxi.do")
	public String doUpdateCihuixinxi(int id,ModelMap map,Cihuixinxi cihuixinxi){
		cihuixinxi=cihuixinxiService.getById(id);
		map.put("cihuixinxi", cihuixinxi);
		return "cihuixinxi_updt";
	}
	
	
	
	
//	后台详细
	@RequestMapping("cihuixinxiDetail.do")
	public String cihuixinxiDetail(int id,ModelMap map,Cihuixinxi cihuixinxi){
		cihuixinxi=cihuixinxiService.getById(id);
		map.put("cihuixinxi", cihuixinxi);
		return "cihuixinxi_detail";
	}
//	前台详细
	@RequestMapping("wjxxDetail.do")
	public String wjxxDetail(int id,ModelMap map,Cihuixinxi cihuixinxi){
		cihuixinxi=cihuixinxiService.getById(id);
		map.put("cihuixinxi", cihuixinxi);
		return "cihuixinxidetail";
	}
//	
	@RequestMapping("updateCihuixinxi.do")
	public String updateCihuixinxi(int id,ModelMap map,Cihuixinxi cihuixinxi,HttpServletRequest request,HttpSession session){
		cihuixinxiService.update(cihuixinxi);
		
		session.setAttribute("backxx", "操作成功");session.setAttribute("backurl", request.getHeader("Referer"));return "redirect:postback.jsp";
	}

//	分页查询
	@RequestMapping("cihuixinxiList.do")
	public String cihuixinxiList(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Cihuixinxi cihuixinxi, String zhongwenmingcheng, String fangyanmingcheng, String yinjie, String waiwenmingcheng, String yinpin, String xiangqing){
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 5);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 5);
		
		
		if(zhongwenmingcheng==null||zhongwenmingcheng.equals("")){pmap.put("zhongwenmingcheng", null);}else{pmap.put("zhongwenmingcheng", zhongwenmingcheng);}
		if(fangyanmingcheng==null||fangyanmingcheng.equals("")){pmap.put("fangyanmingcheng", null);}else{pmap.put("fangyanmingcheng", fangyanmingcheng);}
		if(yinjie==null||yinjie.equals("")){pmap.put("yinjie", null);}else{pmap.put("yinjie", yinjie);}
		if(waiwenmingcheng==null||waiwenmingcheng.equals("")){pmap.put("waiwenmingcheng", null);}else{pmap.put("waiwenmingcheng", waiwenmingcheng);}
		if(yinpin==null||yinpin.equals("")){pmap.put("yinpin", null);}else{pmap.put("yinpin", yinpin);} 
		if(xiangqing==null||xiangqing.equals("")){pmap.put("xiangqing", null);}else{pmap.put("xiangqing", xiangqing);}
		
		int total=cihuixinxiService.getCount(pmap);
		pageBean.setTotal(total);
		List<Cihuixinxi> list=cihuixinxiService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "cihuixinxi_list";
	}
	 
	
	@RequestMapping("cihuixinxiList2.do")
	public String cihuixinxiList2(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Cihuixinxi cihuixinxi, String zhongwenmingcheng, String fangyanmingcheng, String yinjie, String waiwenmingcheng, String yinpin, String xiangqing){
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 50);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 50);
		
		
		if(zhongwenmingcheng==null||zhongwenmingcheng.equals("")){pmap.put("zhongwenmingcheng", null);}else{pmap.put("zhongwenmingcheng", zhongwenmingcheng);}
		if(fangyanmingcheng==null||fangyanmingcheng.equals("")){pmap.put("fangyanmingcheng", null);}else{pmap.put("fangyanmingcheng", fangyanmingcheng);}
		if(yinjie==null||yinjie.equals("")){pmap.put("yinjie", null);}else{pmap.put("yinjie", yinjie);}
		if(waiwenmingcheng==null||waiwenmingcheng.equals("")){pmap.put("waiwenmingcheng", null);}else{pmap.put("waiwenmingcheng", waiwenmingcheng);}
		if(yinpin==null||yinpin.equals("")){pmap.put("yinpin", null);}else{pmap.put("yinpin", yinpin);} 
		if(xiangqing==null||xiangqing.equals("")){pmap.put("xiangqing", null);}else{pmap.put("xiangqing", xiangqing);}
		
		int total=cihuixinxiService.getCount(pmap);
		pageBean.setTotal(total);
		List<Cihuixinxi> list=cihuixinxiService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "cihuixinxi_list2";
	}
	 
	
	@RequestMapping("deleteCihuixinxi.do")
	public String deleteCihuixinxi(int id,HttpServletRequest request,HttpSession session){
		cihuixinxiService.delete(id);
		session.setAttribute("backxx", "删除成功");session.setAttribute("backurl", request.getHeader("Referer"));return "redirect:postback.jsp";
	}
	
	
}
